<?php

/**
 * Model对象生成代理类
 */
class Model {

    function __construct() {
	
    }

    function __destruct() {
	
    }

	protected static function setcmslog($type = 0, $data = array(), $info = "")
	{
		switch($type)
		{
			case 1:
				//补偿金币、经验、矮人果
				//self::setcmslog(1, array("uid" => $uid, "xp" => intval($nlevelconf['boutxp']), "gb" => intval($nlevelconf['needgb']), "mb" => 0, "ps" => intval($nlevelconf["boutps"])), "stuff->uplevel({$uid}, {$typeid}, {$stuffid})");
				break;
			case 2:
				//新用户初始化
				//self::setcmslog(2, array("oid" => $oid, "uid" => $uid), "reg->initfields faild.");
				break;
			case 3:
				//建造完成了，定时收获没有设置
				//self::setcmslog(3, array("uid" => $uid, "spid" => $spid), "stuff->installok_bysp({$uid}, {$spid})");
				//self::setcmslog(3, array("uid" => $uid, "typeid" => $typeid, "stuffid" => $stuffid), "stuff->installok({$uid}, {$typeid}, {$stuffid})");
				break;
			case 4:
				//需要建造时间的，建造完成了，功能没开
				//self::setcmslog(4, array("uid" => $uid, "isbridge" => $uinfo['timemark']['isbridge']), "stuff->installok({$uid}, {$typeid}, {$stuffid})");
				break;
			case 5:
				//不需要建造时间的，建造完成了，小游戏没开
				//self::setcmslog(5, array("uid" => $uid, "gameid" => $gameid), "map->newstuffs({$uid}, data) data -> " . print_r($data, true));
				break;
			case 6:
				//增加材料失败
				//self::setcmslog(6, array("uid" => $uid, "data" => $data), "gold->digtile({$uid}, {$mapid}, {$index})");
				break;
			case 7:
				//合成失败
				//self::setcmslog(7, array("uid" => $uid, "data" => $data), "gold->digtile({$uid}, {$mapid}, {$index})");
			case 8:
				//包裹失败
				//self::setcmslog(8, array("typeid" => 1), "intobag->($uid, $stuffid, $typeid).");
			case 9:
				//抢公主失败
				//self::setcmslog(9, array("uid" => $uid, "fuid" => $fuid), "friend->allureprincess($uid = 0, $fuid = 0)");
				break;
		}
		
		$data['logtype'] = $type;
		$data['info'] = $info;
		$content = json_encode($data);

		if (!($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) return false;
		socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array(
			"sec" => UDP_TIMEOUT_SEC, 
			"usec" => UDP_TIMEOUT_USEC
			));
		socket_sendto($socket, $content, strlen($content), 0, CMSOP_HOST, CMSOP_PORT);
		socket_close($socket);
    }

	protected static function seterrlog($info = "")
	{
    }

	protected static function randpercent($percent1 = 0, $percent2 = 0)
	{
		if($percent1 > $percent2)
		{
			$n = $percent2;
			$percent2 = $percent1;
			$percent1 = $n;
		}

		$n = rand(1, 100);
		if($n >= $percent1 && $n <= $percent2) return true;
		else return false;
	}

	protected static function randpercentdual($percent = array())
	{
		$ret = false;

		$p = array(); $sp = 0;
		foreach($percent as $i => $n)
		{
			$p[$i] = array($sp, $sp + $n);
			$sp = $sp + $n;
		}

		$n = rand(1, 100);
		foreach($p as $i => $arr)
		{
			if($n > $arr[0] && $n <= $arr[1])
			{
				$ret = $i;
				break;
			}
		}

		return $ret;
	}

	protected static function strToHex($string)   
	{
		$hex = "";
		for($i = 0; $i < strlen($string); $i++) $hex .= dechex(ord($string[$i]));
		$hex=strtoupper($hex);
		return $hex;
	}

	protected static function hexToStr($hex)
	{   
		$string="";
		for($i = 0; $i < strlen($hex) - 1; $i += 2) $string .= chr(hexdec($hex[$i].$hex[$i+1]));
		return $string;
	}

}